package com.example.lala.mvp.view.fragment;


import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.lala.R;
import com.example.lala.adapter.DataAdapter;
import com.example.lala.bean.Ban;
import com.example.lala.bean.Data;
import com.example.lala.bean.Money;
import com.example.mylibrary.mvp.view.BaseFragment;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Pig1Fragment extends BaseFragment {
    private static final String TAG = "TAG";
    private String url = "http://api.city2sky.cn/api/WeekTest/getStatisticalInfo";
    String url1 = "http://api.city2sky.cn/api/WeekTest/getProducts";
    private Banner ban;
    private List<Money.DataBean> money=new ArrayList<>();
    private List<Data.DataBean> data = new ArrayList<>();
    List<String> list = new ArrayList<>();
    private RecyclerView rv;

    @Override
    public int bondLayout() {
        return R.layout.fragment_pig1;
    }

    @Override
    public void initView() {
        ban = (Banner) findViewById(R.id.ban);
        initData();
        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        data();
    }

    @Override
    public void initData() {
        OkGo.<String>get("http://musicapi.leanapp.cn/banner")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body();
                        Ban ban1 = new Gson().fromJson(body, Ban.class);
                        List<Ban.BannersBean> banners = ban1.getBanners();
                        for (int i = 0; i < banners.size(); i++) {
                            list.add(banners.get(i).getPicUrl());
                        }
                        Log.i(TAG, "onSuccess: =====" + list);
                        ban.setImages(list).setImageLoader(new ImageLoader() {
                            @Override
                            public void displayImage(Context context, Object path, ImageView imageView) {
                                Glide.with(context).load(path).into(imageView);
                            }
                        }).start();

                    }
                });
    }

    //数据统计接口
    public void data() {
        OkGo.<String>get(url)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body();
                        Data data1 = new Gson().fromJson(body, Data.class);
                        Data.DataBean data2 = data1.getData();
                        data.add(data2);
                        DataAdapter dataAdapter = new DataAdapter(R.layout.data_item, data);
                        rv.setAdapter(dataAdapter);
                    }
                });

    }
    public void product(){
        OkGo.<String>get(url1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body();



                    }
                });
    }

    @Override
    public void Toast(String s) {

    }
}
